package com.anssy.voteballot.utils;

import android.media.MediaPlayer;

import java.io.IOException;

public class MediaPlayerUtils {


    public static MediaPlayer mPlayer;

    public static MediaPlayer playSoundFromA(String audioPath) {

        if (mPlayer == null) {
            mPlayer = new MediaPlayer();
        }
        if (mPlayer.isPlaying()) {
            mPlayer.stop();
        }
        mPlayer.reset();
        try {
            mPlayer.setDataSource(audioPath);
            mPlayer.prepareAsync();
            mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mPlayer.start();
                }
            });

        } catch (IOException e) {
            e.printStackTrace();
        }
        return mPlayer;
    }

    public static void stopMediaPlayer() {
        if (mPlayer != null) {
            mPlayer.stop();
        }
    }

}
